import datetime
import os
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from tkinter.messagebox import showinfo


class Window(tk.Tk):
    def __init__(self):
        super(Window, self).__init__()
        self.video_path = None
        self.mp3_path_root = None
        self.script = 'xhhd_desk.py'
        self.create_widget()
        self.mainloop()

    def create_widget(self):
        self.geometry('800x600+300+100')
        btn_1 = ttk.Button(self, text='加载视频', command=self.btn_load_1)
        btn_1.place(relx=0.15, rely=0.1)
        btn_2 = ttk.Button(self, text='选择音频目录', command=self.btn_load_2)
        btn_2.place(relx=0.55, rely=0.1)
        btn_run = ttk.Button(self, text='运行', command=self.run)
        btn_run.place(relx=0.45, rely=0.8)
        # 第一个文本框
        self.iframe1 = ttk.Frame(self, width=100, height=100)
        self.iframe1.place(relx=0.15, rely=0.2)
        self.text1 = tk.Text(self.iframe1, width=30, height=25, wrap="none", bg="lightyellow")
        self.text1.configure(state=tk.DISABLED)
        vbar = tk.Scrollbar(self.iframe1)  # 垂直滚动条
        vbar.pack(side=tk.RIGHT, fill=tk.Y)
        hbar = tk.Scrollbar(self.iframe1, orient=tk.HORIZONTAL)  # 水平滚动条
        hbar.pack(side=tk.BOTTOM, fill=tk.X)
        hbar.configure(command=self.text1.xview)
        self.text1.config(xscrollcommand=hbar.set)
        vbar.configure(command=self.text1.yview)
        # 配置好了滚动条再布局
        self.text1.config(yscrollcommand=vbar.set)
        self.text1.pack(fill=tk.BOTH, expand=True)

        # 第二个文本框
        self.iframe2 = ttk.Frame(self, width=100, height=100)
        self.iframe2.place(relx=0.55, rely=0.2)
        self.text2 = tk.Text(self.iframe2, width=30, height=25, wrap="none", bg="lightyellow")
        self.text2.configure(state=tk.DISABLED)
        vbar = tk.Scrollbar(self.iframe2)  # 垂直滚动条
        vbar.pack(side=tk.RIGHT, fill=tk.Y)
        hbar = tk.Scrollbar(self.iframe2, orient=tk.HORIZONTAL)  # 水平滚动条
        hbar.pack(side=tk.BOTTOM, fill=tk.X)
        hbar.configure(command=self.text2.xview)
        self.text2.config(xscrollcommand=hbar.set)
        vbar.configure(command=self.text2.yview)
        # 配置好了滚动条再布局
        self.text2.config(yscrollcommand=vbar.set)
        self.text2.pack(fill=tk.BOTH, expand=True)

    def btn_load_1(self):
        video = filedialog.askopenfilenames()
        if video:
            self.video_path = video[0]
            self.update_text1(os.path.basename(self.video_path))

    def btn_load_2(self):
        path_root = filedialog.askdirectory()
        if path_root:
            self.mp3_path_root = path_root
            mp3 = [i for i in os.listdir(path_root) if i.endswith('.mp3')]
            self.update_text2(mp3)

    def update_text1(self, path):
        self.text1.configure(state=tk.NORMAL)
        self.text1.delete(1.0, tk.END)
        self.text1.insert(tk.END, path)
        self.text1.configure(state=tk.DISABLED)

    def update_text2(self, mp3s):
        self.text2.configure(state=tk.NORMAL)
        self.text2.delete(1.0, tk.END)
        for mp3 in mp3s:
            self.text2.insert(tk.END, mp3)
            self.text2.insert(tk.END, '\n')
        self.text2.configure(state=tk.DISABLED)

    def run(self):
        if not self.video_path or not self.mp3_path_root:
            showinfo('提示','请选择相关视频和音频!')
            return
        paths = [i for i in self.text2.get(1.0, tk.END).split('\n') if i]
        for path in paths:
            dir_name = str(datetime.datetime.now().strftime("%Y%m%d%H%M"))
            file_name = str(datetime.datetime.now().strftime("%Y%m%d_%H%M%S"))+'.mp4'
            if not os.path.exists(dir_name):
                os.mkdir(dir_name)
            cmd = f'python {self.script} --face {self.video_path} --audio {os.path.join(self.mp3_path_root, path)} --outfile {os.path.join(dir_name, file_name)} --bg_upsampler Non 2>&1 &'
            os.system(cmd)
        showinfo('提示', '脚本已成功执行!')


Window()
